package com.zzzs.synchronizeddemo;

import org.openjdk.jol.info.ClassLayout;

import java.time.LocalDateTime;

/**
 * @author zhu_shuai
 * @since 7/28
 */
public class SynchronizedDemo {

    private static Object o;

    public static void main(String[] args) throws InterruptedException {
        o = new Object();
        System.out.println(ClassLayout.parseClass(Object.class).toPrintable());
        System.out.println(ClassLayout.parseInstance(o).toPrintable());
        synchronized (o) {
            System.out.println("------------加锁--------------");
            System.out.println(ClassLayout.parseInstance(o).toPrintable());
            Thread.sleep(3*1000);
            synchronized (o) {
                Thread.sleep(3*1000);
                System.out.println("------------重入锁--------------");
                System.out.println(ClassLayout.parseInstance(o).toPrintable());
            }
        }
        for (int i = 0; i < 10; i++) {
            new Thread(SynchronizedDemo::run).start();
        }
    }

    private static void run() {
        synchronized (o) {
            System.out.printf("ThreadName: %s time: %s %n",
                    Thread.currentThread().getName(),
                    LocalDateTime.now().toString());
            System.out.println(ClassLayout.parseInstance(o).toPrintable());
        }
    }
}
